/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Window;

import cppgrasp.CPPGrasp;
import estructuras.TArista;
import estructuras.TNodo;
import java.io.IOException;
import java.util.ArrayList;

/**
 *
 * @author yulian
 */
public class PanelPrincipal extends javax.swing.JPanel {

    /**
     * Creates new form FramePrincipal
     */
    public PanelPrincipal() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        buttomStart = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        txtalfa = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtSolution = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        txtDir = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtNodo = new javax.swing.JTextField();

        jLabel1.setText("Bienvenido a AreYouRudy!");

        buttomStart.setText("Comenzar");
        buttomStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttomStartActionPerformed(evt);
            }
        });

        jLabel2.setText("Ingrese el valor del coheficiente de relajación :");

        txtalfa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtalfaActionPerformed(evt);
            }
        });

        jLabel3.setText("Solución Construída :");

        jLabel4.setText("alpha                 =");

        jLabel5.setText("Método de Construcción GRASP");

        txtSolution.setColumns(20);
        txtSolution.setRows(5);
        jScrollPane1.setViewportView(txtSolution);

        jLabel6.setText("Ingrese la dirección del archivo del grafo:");

        jLabel7.setText("nodo inicial        = ");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(222, 222, 222)
                .addComponent(jLabel1)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDir))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(39, 39, 39)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtalfa, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
                                    .addComponent(txtNodo))
                                .addGap(18, 18, 18)
                                .addComponent(buttomStart)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addGap(19, 19, 19))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabel1)
                .addGap(17, 17, 17)
                .addComponent(jLabel5)
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtDir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtalfa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttomStart)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel7)))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtNodo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void txtalfaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtalfaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtalfaActionPerformed

    private void buttomStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttomStartActionPerformed
       
       int nodoInicial= Integer.parseInt(txtNodo.getText()); 
       String dir  = txtDir.getText();
       double alfa = Double.parseDouble(txtalfa.getText());
       
       txtSolution.setText("");
      
       try {
            //Leemos el archivo y obtenemos la data del grafo
            TArista grafo[][] = CPPGrasp.serv.leerArchivoGrafo(dir);       
            ArrayList<Integer> camino = new ArrayList<>();
            ArrayList<TNodo> nodosConectados = new ArrayList<>();  
       
            //seteamos ciertas variables necesarias
            CPPGrasp.serv.setAlpha(alfa);
            CPPGrasp.serv.setNodoInicial(nodoInicial);
       
            //Genera un arreglo de los vertices por los cuales pasó el "cartero"
            ArrayList<Integer> sParcial = CPPGrasp.serv.AlgConstGrasp(grafo,camino,nodosConectados);
       
            //Genera la cadena solucion
            String cadSolucion = CPPGrasp.serv.GeneraCadenaSolucion(sParcial); 
       
            //Muestra el resultado de la construccion
            txtSolution.setText(cadSolucion);
            
       } catch (IOException ex) {
           txtSolution.setText("Ocurrio un error en la lectura: " + ex.getMessage());
       }       
    }//GEN-LAST:event_buttomStartActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttomStart;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField txtDir;
    private javax.swing.JTextField txtNodo;
    private javax.swing.JTextArea txtSolution;
    private javax.swing.JTextField txtalfa;
    // End of variables declaration//GEN-END:variables
}
